{% extends 'xadmin/base.html' %}
{% load staticfiles %}

{% block title %}
    模版管理
{% endblock %}

{% block css %}
    <link href="{% static 'xadmin/css/plugins/toastr/toastr.min.css' %}" rel="stylesheet">
{% endblock %}

{% block btn %}
    <button type="button" class="btn btn-xs btn-outline btn-primary" onclick="window.location.href='{% url 'templateadd' %}?siteid={{ siteid }}'">添加模版</button>
{% endblock %}

{% block main %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-lg-3 col-lg-offset-9">
                {% if request.user.is_superuser %}
                    <button type="button" class="btn btn-xs btn-primary" onclick="window.location.href='{% url 'templateadd' %}?siteid={{ siteid }}'"><i class="fa fa-plus"></i>新增模版</button>
                    <button type="button" class="btn btn-xs btn-danger" onclick="templates_del()"><i class="glyphicon glyphicon-remove"></i>删除</button>
                {% else %}
                    {% for perm_type, perm_slug_list in perm_dic.items %}
                        {% if perm_type == 'template' %}
                            {% if 'add' in perm_slug_list %}
                                <button type="button" class="btn btn-xs btn-primary" onclick="window.location.href='{% url 'templateadd' %}?siteid={{ siteid }}'"><i class="fa fa-plus"></i>新增模版</button>
                            {% endif %}
                            {% if 'delete' in perm_slug_list %}
                                <button type="button" class="btn btn-xs btn-danger" onclick="templates_del()"><i class="glyphicon glyphicon-remove"></i>删除</button>
                            {% endif %}
                        {% endif %}
                    {% endfor %}
                {% endif %}


            </div>
        </div>
        <div class="row">
                <div class="col-lg-12">
                    <div class="ibox float-e-margins">
                        <div class="ibox-content" style="border-width:0px 0px">
                            <div class="table-responsive">
                                <table class="table table-striped">
                                    <thead>
                                        <tr>
                                            <th>
                                                <div class="icheckbox_square-green" style="position: relative;">
                                                    <input type="checkbox" class="i-checks checkall" name="input[]" style="position: absolute; opacity: 0;">
                                                </div>
                                            </th>
                                            <th>名称</th>
                                            <th>路径</th>
                                            <th>图片</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% for template in template_list %}
                                            <tr>
                                                <td>
                                                    <div class="icheckbox_square-green" style="position: relative;">
                                                        <input type="checkbox" class="i-checks" value="{{ template.id }}" name="input[]" style="position: absolute; opacity: 0;">
                                                    </div>
                                                </td>
                                                <td>{{ template.name }}</td>
                                                <td>{{ template.path }}</td>
                                                <td >
                                                    <img style="width:50px;" src="/media/{{ template.picture }}" data-toggle="popover" data-trigger="hover" data-placement="right"  data-title=""  data-html='true' data-content="<img style='width:200px;' src='/media/{{ template.picture }}' />" />
                                                </td>
                                                <td>
                                                    {% if request.user.is_superuser %}
                                                        <a  class="btn btn-xs btn-outline btn-primary"  href="{% url 'templateedit' template.id %}?siteid={{ siteid }}">编辑</a>
                                                        <a  class="btn btn-xs btn-outline btn-warning" onclick="template_del({{ template.id }}, {{ siteid }})">删除</a>
                                                        <a  class="btn btn-xs btn-outline btn-primary"  href="{% url 'editcode' template.id %}?siteid={{ siteid }}">编辑代码</a>
                                                    {% else %}
                                                        {% for perm_type, perm_slug in perm_dic.items %}
                                                            {% if perm_type == 'template' %}
                                                                {% if 'update' in perm_slug %}
                                                                    <a  class="btn btn-xs btn-outline btn-primary"  href="{% url 'templateedit' template.id %}?siteid={{ siteid }}">编辑</a>
                                                                {% endif %}
                                                                {% if 'delete' in perm_slug %}
                                                                    <a  class="btn btn-xs btn-outline btn-warning" onclick="template_del({{ template.id }}, {{ siteid }})">删除</a>
                                                                {% endif %}
                                                            {% endif %}
                                                        {% endfor %}

                                                    {% endif %}

                                                </td>
                                                </td>
                                            </tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                            </div>

                        </div>
                    </div>
                </div>

            </div>
        </div>
{% endblock %}

{% block script %}
    <script src="{% static 'xadmin/js/plugins/toastr/toastr.min.js' %}"></script>
    <script src="{% static 'xadmin/js/myjs/toastr_customer.js' %}"></script>
    <script>
        $(document).ready(function () {
            $('.i-checks').iCheck({
                checkboxClass: 'icheckbox_square-green',
                radioClass: 'iradio_square-green',
            });
            //全选操作
            $('.checkall').on('ifChecked', function(event){
                $('.i-checks:not(:first)').iCheck('check');
            });
            $('.checkall').on('ifUnchecked', function(event){
                $('.i-checks:not(:first)').iCheck('uncheck');
            });
            //初始化弹出图片框
            $('[data-toggle="popover"]').popover()

        });

        function template_del(templateid, siteid){
            $.ajax({
                url: '{% url 'templatedel' %}',
                type: 'post',
                dataType: 'json',
                data: {'templateid': templateid, 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                success: function(bk){
                    console.log(bk);
                    if(bk.status == '200'){
                        send_success_message(bk.message);
                    }else{
                        toastr.error(bk.message);
                    }
                }
            })
        }

        function templates_del(){
            var id_list = [];
            $('.i-checks:not(:first)').each(function(){
                if($(this).get(0).checked){
                    id_list.push($(this).val());
                }
            });
            if(id_list.length > 0){
                layer.confirm('确定要删除吗?', function(){
                    $.ajax({
                        url: '{% url "templatesdel" %}?siteid={{ siteid }}',
                        type: 'post',
                        dataType: 'json',
                        data: {'templateids': id_list.join(','), 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                        success: function(bk){
                            if(bk.status == '200'){
                                send_success_message(bk.msg);
                            }
                            else{
                                toastr.error(bk.message);
                            }
                        }
                    })
                })

            }else{
                toastr.error('请选择要删除的组');
            }
        }
    </script>
{% endblock %}

